<?php
class Front_InfoController extends ExtLb_Controller_Action
{
	private $userTbl;
	private $studentTbl;
	
	private $loginForm;
	private $userForm;
	
    public function init()
    {
        $this->loadLayout('default', 'layout');
        $this->loadCss('default', 'info');
//   		$this->loadJs("/quiz/public/js/ui/jquery.ui.mouse.js");
//   		$this->loadJs("/quiz/public/js/ui/jquery.ui.draggable.js");
//   		$this->loadJs("/quiz/public/js/ui/jquery.ui.button.js");
//   		$this->loadJs("/quiz/public/js/ui/jquery.ui.position.js");
//   		$this->loadJs("/quiz/public/js/ui/jquery.ui.resizable.js");
//   		$this->loadJs("/quiz/public/js/ui/jquery.ui.dialog.js");
//   		$this->loadJs("/quiz/public/js/ui/jquery.effects.core.js");
        
        $this->userForm 	= new Admin_Form_UserForm();
        $this->loginForm	= new Front_Form_LoginForm();
        $this->userTbl		= new Admin_Model_User();
        $this->studentTbl  	= new Admin_Model_Student();
        
        $this->view->loginForm	= $this->loginForm;
        
    }

    public function indexAction()
    {
    	if(!Zend_Auth::getInstance()->hasIdentity()){
    		$this->jDialogMessage('Thông báo','Hãy đăng nhập để có thể sử dụng chức năng này');
    		$this->_forward('index','index','front');
    	}else{
	    	//GET INFO HAD LOGIN
	    	$auth = Zend_Auth::getInstance()->getStorage()->read(); 
	    	$username = $auth->username;
	    	
	    	$this->userForm->addAction_Url('front','info', 'edit');
	    	$this->userForm->addSubmitButton('cmdEdit', 'Cập nhập');
	    	
	    	$student = $this->studentTbl->selectStudent("sv.username = '".$username."'")
	    								->query()
	    								->fetchAll();
	    								
	    	if(!empty($student)){							
		    	$this->userForm->getElement('txtUser')
		    					->setAttrib('readonly', 'ok')
		    					->setValue($student[0]['username'])
		    					->setAttrib('readonly', '');
		    	$this->userForm->getElement('txtPass')->setValue($student[0]['password']);
		    	$this->userForm->getElement('txtHoDem')->setValue($student[0]['hoDem']);
		    	$this->userForm->getElement('txtTen')->setValue($student[0]['ten']);
		    	$this->userForm->getElement('cbbRole')->setValue($student[0]['quyen'])->setAttrib('disable', 'disable');
		    	$this->userForm->getElement('txtNgaySinh')->setValue($student[0]['ngaySinh']);
		    	$this->userForm->getElement('txtEmail')->setValue($student[0]['email']);
		    	$this->userForm->getElement('cbbLop')->setValue($student[0]['idLop'])->setAttrib('disable', 'disable');
	    	}
	    	
	    	$this->view->userForm	= $this->userForm;
	    	$this->view->student	= $student;
    	
    	}    	
    }

    public function editAction()
    {
    	if(isset($_POST['cmdEdit'])){
	    	$user 		= $this->_getParam('txtUser');
	    	$pass 		= $this->_getParam('txtPass');
	    	$hoDem 		= $this->_getParam('txtHoDem');
	    	$ten 		= $this->_getParam('txtTen');
	    	$email		= $this->_getParam('txtEmail');
	    	$ngaySinh 	= $this->_getParam('txtNgaySinh');
	    	$quyen 		= $this->_getParam('cbbRole');
	    	$lop		= $this->_getParam('cbbLop');
	    			
	    	$this->userTbl->updateUser($pass, $quyen, $hoDem, $ten,$email, $ngaySinh,$user);
	    	
	    	//if da co thi cap nhap, chua co thi them
	    	$dataStudents = $this->studentTbl->fetchSql("select username from sinhvien");

	    	if($this->in_array($user, $dataStudents,'username')){
    			$this->studentTbl->updateStudent($user, $lop);
	    	}else{
 				$this->studentTbl->addStudent($user, $lop);	
	    	}
	    	
	    	$this->jDialogMessage('(n_n) Thông báo','Thành công!!!');
    	}
    	$this->_forward('index','info','front');
    }

	private function in_array($valueCheck,$arrayCheck,$stringKey){
		foreach ($arrayCheck as $key=>$value) {
			if( trim($value[$stringKey]) == trim($valueCheck)) return TRUE;
		}
		return FALSE;
	}

}









